from app import db


class Position(db.EmbeddedDocument):
    longitude = db.FloatField()
    latitude = db.FloatField()

    def json(self):
        return {
            "longitude": self.longitude,
            "latitude": self.latitude
        }


class Student(db.Document):
    studentId = db.StringField()
    roomId = db.IntField()
    phone = db.StringField()
    name = db.StringField()
    status = db.StringField()
    isChecked = db.BooleanField()
    warnOpened = db.BooleanField()
    position = db.EmbeddedDocumentField(Position)
    meta = {'collection': 'students', 'strict': False}

    @classmethod
    def get_all(cls):
        result = cls.objects()
        return result

    @classmethod
    def get_by_studentId(cls, studentId):
        return cls.objects(studentId=studentId).first()

    @classmethod
    def get_by_roomId(cls, roomId):
        return cls.objects(roomId=roomId)

    @classmethod
    def set_roomId(cls, studentId, roomId):
        cls.objects(studentId=studentId).update(roomId=roomId)
        return

    @classmethod
    def set_status(cls, studentId, status):
        cls.objects(stutdent_id=studentId).update(status=status)
        return

    @classmethod
    def update_isChecked(cls, studentId, isChecked):
        cls.objects(studentId=studentId).update(isChecked=isChecked)
        return

    @classmethod
    def get_check(cls):
        checked = len(cls.objects(isChecked=True))
        unchecked = len(cls.objects(isChecked=False))
        return checked, unchecked

    @classmethod
    def get_unchecked(cls):
        return cls.objects(isChecked=False)

    def json(self):
        return {
            "studentId": self.studentId,
            "roomId": self.roomId,
            "name": self.name,
            "status": self.status,
            "isChecked": self.isChecked,
            "position": self.position.json()
        }
